package com.itheima.service.impl;

import com.itheima.mapper.PeopleMapper;
import com.itheima.pojo.People;
import com.itheima.pojo.Result;
import com.itheima.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PeopleServiceImpl implements PeopleService {
    @Autowired
    private PeopleMapper peopleMapper;
    @Override
    public List<People> getPeopleList() {
        List<People> people = peopleMapper.searchPeople(new People());
        for (int i = 0; i < people.size(); i++) {
            people.get(i).setPassword("");
        }
        return people;
    }

    @Override
    public People getPersonalInformation(People people) {
        People peoples = peopleMapper.searchPeople(people).get(0);
        peoples.setPassword("");

        return peoples;
    }

    @Override
    public List<People> searchPeople(People people) {
        return peopleMapper.searchPeople(people);
    }

    @Override
    public Result changePassword(String oldPassword, String newPassword, Integer id) {
        People p=new People();
        p.setId(id);
        People people = peopleMapper.searchPeople(p).get(0);
        if (!oldPassword.equals(people.getPassword())) {
            return Result.error("原密码错误");
        }
        if (oldPassword.equals(newPassword)) {
            return Result.error("新密码与原密码相同");
        }
        people.setPassword(newPassword);
        peopleMapper.update(people);
        return Result.success();
    }

    @Override
    public void changePersonalInformation(People people) {
        if (people.getName().isEmpty()) {
            people.setName(null);
        }
        if (people.getMajor().isEmpty()) {
            people.setMajor(null);
        }
        if (people.getClassAndGrade().isEmpty()) {
            people.setClassAndGrade(null);
        }
        if (people.getField().isEmpty()) {
            people.setField(null);
        }
        peopleMapper.update(people);
    }

    @Override
    public void setPhone(People people) {
        peopleMapper.update(people);
    }

    @Override
    public Integer countHouduan() {
        List<People> people =  peopleMapper.searchPeople(new People());
        int count = 0;
        for (int i = 0; i < people.size(); i++) {
            if (people.get(i).getField().equals("后端")){
                count++;
            }
        }
        return count;
    }

    @Override
    public Integer countQianduan() {
        List<People> people =  peopleMapper.searchPeople(new People());
        int count = 0;
        for (int i = 0; i < people.size(); i++) {
            if (people.get(i).getField().equals("前端")){
                count++;
            }
        }
        return count;

    }

    @Override
    public Integer countQianrushi() {
        List<People> people =  peopleMapper.searchPeople(new People());
        int count = 0;
        for (int i = 0; i < people.size(); i++) {
            if (people.get(i).getField().equals("嵌入式")){
                count++;
            }
        }
        return count;
    }
}
